<?php 
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Account extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function login()
    {
        $this->load->view("home/account/login");
    }
    function register()
    {
        $this->load->view("home/account/register");
    }
    
    function account_ads()
    {
        if($this->session->userdata('userID') != '')
        {
            $userID = $this->session->userdata('userID');
        }
        else//demo
        {
            $page = 1;
            $userID = 11;
            if($this->uri->segment(4) != '')
            {
                $page = $this->uri->segment(4);
            }
            $sql = "SELECT adsID,adsTitle,adsView,adsPostDate,adsExpireDate,adsStatus FROM {PRE}ads WHERE adsUser = $userID";
            $data['numrows'] = count($this->global_model->fetch($sql));
            $data['numpages'] = ceil(count($this->global_model->fetch($sql))/2);
            $data['currentpage'] = $page;
            $sql .=" ORDER BY adsView DESC limit ".(($page-1)*2)." ,2";
            $data['rowAds'] = $this->global_model->fetch($sql);
            
            $this->load->view("home/account/ads/account_ads", $data);
            //$this->login();
        }
    }
    
    function account_ads_favorite()
    {
        if($this->session->userdata('userID') != '')
        {
            $userID = $this->session->userdata('userID');
        }
        else//demo
        {
            $page = 1;
            $userID = 11;
            if($this->uri->segment(4) != '')
            {
                $page = $this->uri->segment(4);
            }
            $sql = "SELECT {PRE}ads.adsID,{PRE}ads.adsTitle,{PRE}ads.adsView,{PRE}province.preName,{PRE}ads_favorite.adfAddDate ";
            $sql .="FROM {PRE}ads, {PRE}ads_favorite, {PRE}province ";
            $sql .= "WHERE {PRE}ads_favorite.adfUser = $userID AND {PRE}ads_favorite.adfUser = {PRE}ads.adsUser AND {PRE}ads.adsProvince = {PRE}province.preID";
            
            $data['numrows'] = count($this->global_model->fetch($sql));
            $data['numpages'] = ceil(count($this->global_model->fetch($sql))/2);
            $data['currentpage'] = $page;
            
            $sql .=" ORDER BY {PRE}ads.adsView DESC limit ".(($page-1)*2)." ,2";
            $data['rowAds'] = $this->global_model->fetch($sql);
            
            $this->load->view("home/account/ads/account_ads_favorite", $data);
            //$this->login();
        }
    }
    
    function account_ads_post()
    { 
        //select data for control form   
        $sql = "SELECT preID,preName FROM {PRE}province";
        $data['rowProvince'] = $this->global_model->fetch($sql);
        $sql = "SELECT secID,secName FROM {PRE}section";
        $data['rowSection'] = $this->global_model->fetch($sql);
        $sql = "SELECT catID,catName FROM {PRE}category";
        $data['rowCategory'] = $this->global_model->fetch($sql);
        
        if($this->session->userdata('userID') != '')
        {
            //get data from client
            $adsUser = $this->session->userdata('userID');
            $title = $this->input->post('txtTitle');
            $province = $this->input->post('txtProvince');
            $section = $this->input->post('txtSection');
            $category = $this->input->post('txtCategory');
            $expiredate = $this->input->post('txtExpireDate');
            $detail = $this->input->post('txtDetail');
            $image = "";
            if(isset($_FILES['txtImage']))
            {
                $image = $_FILES['txtImage']['name'];
                $this->global_model->upload_image("txtImage", IMAGEADS_PATH);
            }
            
            $fullname = $this->input->post('txtFullname');
            $address = $this->input->post('txtAddress');
            $phone = $this->input->post('txtPhone');
            $mobile = $this->input->post('txtMobile');
            $fax = $this->input->post('txtFax');
            $email = $this->input->post('txtEmail');
            $yahoo = $this->input->post('txtYahoo');
            $skype = $this->input->post('txtSkype');
            $codeconfirm = $this->input->post('txtCaptcha');
            
            //create sql query and insert to database
            $sql = "INSERT INTO {PRE}ads(adsUser,adsTitle,adsProvince,adsSection,adsCategory,adsExpireDate,adsDetail,adsImage,adsFullname,adsAddress,adsPhone,adsMobile,adsFax,adsEmail,adsYahoo,adsSkype) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            $this->db->query($sql, array($adsUser,$title,$province,$section,$category,$expiredate,$detail,$image,$fullname,$address,$phone,$mobile,$fax,$email,$yahoo,$skype));
            $this->load->view("home/account/ads/account_ads_post", $data);
            
        }
        else//demo
        {
            $this->load->view("home/account/ads/account_ads_post", $data);
            //$this->login();
        }
    }
}